package com.aconcagua.mvc.factory {
  import com.aconcagua.beans.BeanDefinitionFactory;
  import com.aconcagua.beans.BeanDefinitionSupport;
  import com.aconcagua.beans.ConstructorArg;
  import com.aconcagua.factory.BeanFactory;
  import com.aconcagua.mvc.control.GestureMapping;
  
  import mx.core.IMXMLObject;

  [DefaultProperty("command")]
  public class GestureMapping implements BeanDefinitionFactory, IMXMLObject{
    private var _id: String;
    private var _mapping: BeanDefinitionSupport;
    private var _gesture: String;
    private var _command: Command;
    private var _handlerMethod: String;

    public function GestureMapping() {
      _handlerMethod = "execute";
    }

    public function set gesture(name: String): void {
      _gesture = name;
    }

    public function set command(theCommand: Command): void {
      _command = theCommand;
    }

    public function set handlerMethod(name: String): void {
      _handlerMethod = name;
    }

    public function initialized(document:Object, id:String): void {
      _id = id;
    }

    public function registerIn(beanFactory: BeanFactory): void {
      //Support for nested commands (no need to declare them outside the mapping):
      if (!_command.isRegisteredIn(beanFactory)) {
        _command.registerIn(beanFactory);
      }

      _mapping = new BeanDefinitionSupport(com.aconcagua.mvc.control.GestureMapping, _id);
      _mapping.scope = BeanDefinitionSupport.SCOPE_SINGLETON;
      _mapping.addConstructorArg(new ConstructorArg(_gesture));
      _mapping.addConstructorArg(new ConstructorArg(_command.id));
      _mapping.addConstructorArg(new ConstructorArg(_handlerMethod));
      
      beanFactory.registerBeanDefinition(_mapping)
    }
    
    
    public function isRegisteredIn(beanFactory: BeanFactory): Boolean {
      return _mapping != null && beanFactory.containsBean(_mapping.name);
    }
    
    public function retrieveFrom(beanFactory: BeanFactory): Object {
      return beanFactory.getBean(_mapping.name);
    }
  }
}